# Replication archive for:
# Galos and Coppock. 2023. 
# Gender Composition Predicts Gender Bias: A Meta-reanalysis of Hiring Discrimination Audit Experiments. 
# Science Advances. Forthcoming.

library(tidyverse)
library(metafor)
library(broom)
library(scales)
source("code/helpers.R")

occupation_cates <- read_rds("data/occupation_cates_ethnicity.rds")


# Conduct meta-analyses
minority_df <- filter(occupation_cates, ethnicity_fct == "Minority")
majority_df <- filter(occupation_cates, ethnicity_fct == "Majority")

minority_fit <- 
  rma.uni(
  yi = estimate,
  sei = std.error,
  mods = ~ fraction_female, data = minority_df
)
majority_fit <- rma.uni(
  yi = estimate,
  sei = std.error,
  mods = ~ fraction_female, data = majority_df
)

minority_df$weight <- weights(minority_fit)
majority_df$weight <- weights(majority_fit)

gg_df <- 
  bind_rows(
    "Minority" = minority_df,
    "Majority" = majority_df,
    .id = "ethnicity_fct"
  )

preds_df <-
  bind_rows(
    "Minority" = tidy_predictions(minority_fit),
    "Majority" = tidy_predictions(majority_fit),
    .id = "ethnicity_fct"
  )


g <-
  ggplot(gg_df, aes(fraction_female, estimate, color = ethnicity_fct, group = ethnicity_fct, shape = ethnicity_fct)) +
  geom_hline(yintercept = 0, linetype = "dashed", alpha = 0.35) +
  geom_linerange(aes(ymin = conf.low, ymax = conf.high), alpha = 0.1) +
  geom_point(alpha = 0.2, aes(size = weight), stroke = 0) +
  geom_ribbon(data = preds_df, aes(ymin = conf.low, ymax = conf.high, fill = ethnicity_fct), alpha = 0.35) +
  geom_line(data = preds_df, alpha = 0.55) +
  coord_cartesian(xlim = c(0, 1), ylim = c(-25, 25)) +
  scale_size_continuous(range = c(1, 4), guide = "none") +
  scale_y_continuous(breaks = seq(-10, 10, 5)) +
  scale_x_continuous(label = percent_format()) +
  scale_color_brewer(palette = "Set1") +
  scale_fill_brewer(palette = "Set1") +
  theme_bw() +
  theme(panel.grid.minor = element_blank(),
        legend.position = c(0.875, 0.2),
        legend.background = element_rect(fill = "transparent"),
        legend.title = element_blank()) +
  labs(x = "Status quo gender composition (share of women)",
       y = "Conditional average treatment effect")
g

